package com.cyj.dream.auth.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cyj.dream.core.base.SysBaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @Description: 系统角色管理表
 * @BelongsProject: dream-DreamChardonnay
 * @BelongsPackage: com.cyj.dream.auth.model
 * @Author: ChenYongJia
 * @CreateTime: 2021-04-01 09:58
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("dream_sys_role")
@ApiModel(value = "SysRole", description = "dream_sys_role 角色实体")
public class SysRole extends SysBaseEntity {

    @Column(columnDefinition = "varchar(128) NOT NULL COMMENT '角色名称'  ")
    @ApiModelProperty(value = "角色名称")
    private String sysRoleName;

    @Column(columnDefinition = "varchar(255) NOT NULL COMMENT '角色组'  ")
    @ApiModelProperty(value = "角色组")
    private String sysRoleGroup;

    @Column(columnDefinition = "bigint(20) NOT NULL COMMENT '父节点id'  ")
    @ApiModelProperty(value = "父节点id", example = "1")
    private Long sysRoleParentId;

    @Column(columnDefinition = "varchar(128) NOT NULL COMMENT '角色权限名'  ")
    @ApiModelProperty(value = "角色权限名")
    private String sysRoleTrueName;

    @Column(columnDefinition = "tinyint(3) NOT NULL COMMENT '角色类型（0：开发，1：用户，2：测试）' default 0 ")
    @ApiModelProperty(value = "角色类型（0：开发，1：用户，2：测试）", example = "1")
    private Integer sysRoleType;

    @Column(columnDefinition = "tinyint(3) NOT NULL COMMENT '角色状态(0:启用,1:禁用)' default 0 ")
    @ApiModelProperty(value = "角色状态(0:启用,1:禁用)")
    private Integer sysRoleStatus;

    @Column(columnDefinition = "int(11) NOT NULL COMMENT '角色权重' default 0 ")
    @ApiModelProperty(value = "角色权重", example = "9999")
    private Integer sysRoleWeight;

    @Column(columnDefinition = "varchar(255) NOT NULL COMMENT '角色备注描述'  ")
    @ApiModelProperty(value = "角色备注描述")
    private String sysRoleRemark;

    @JsonIgnore
    @ManyToMany(fetch = FetchType.EAGER) // 多对多关系
    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL }) // 级联关系
    @JoinTable(name = "dream_sys_user_role", // 指定第三张中间表名称
            joinColumns = { @JoinColumn(name = "sys_role_id") }, // 本表主键roleId与第三张中间表user_role_tb的外键user_role_tb_role_id对应.本表与中间表的外键对应关系
            inverseJoinColumns = { @JoinColumn(name = "sys_user_id") }) // 另一张表与中间表的外键的对应关系
    @NotFound(action = NotFoundAction.IGNORE) // NotFound : 意思是找不到引用的外键数据时忽略，NotFound默认是exception
    @TableField(exist = false)
    private Set<SysUser> usersSet = new HashSet<>();

    @Override
    public String toString() {
        return "SysRole{" +
                ", sysRoleName='" + sysRoleName + '\'' +
                ", sysRoleGroup='" + sysRoleGroup + '\'' +
                ", sysRoleParentId=" + sysRoleParentId +
                ", sysRoleTrueName='" + sysRoleTrueName + '\'' +
                ", sysRoleType=" + sysRoleType +
                ", sysRoleStatus=" + sysRoleStatus +
                ", sysRoleWeight=" + sysRoleWeight +
                ", sysRoleRemark='" + sysRoleRemark + '\'' +
                '}';
    }

}
